﻿Imports System.Net
Imports LukeSw.Windows.Forms

Delegate Sub SetTextCallback(ByVal [text] As String)
Delegate Sub SetVisCallback(ByVal [text] As Boolean)

Public Class frmInternetSpeed
    Private Sub btnCheckSpeed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckSpeed.Click
        Me.rtbResults.Text = "Checking Speed...."
        Dim thrMyThread As New System.Threading.Thread( _
    AddressOf InetSpeed)
        thrMyThread.Start()
    End Sub

    Private Sub InetSpeed()
        Dim generator As New Random
        Dim randomValue As Integer
        randomValue = generator.Next(1, 9999999)
        ' the URL to download a file from
        Dim URL As New Uri("http://download.meamod.com/speedtest/2046.stf?" & randomValue)
        Dim wc As New WebClient()
        Dim NewText As String
        ' get current tickcount 
        Dim starttime As Double = Environment.TickCount

        ' download file from the specified URL, and save it to C:\speedtest.txt
        Try
            wc.DownloadData(URL)

            ' get current tickcount
            Dim endtime As Double = Environment.TickCount

            ' how many seconds did it take?
            ' we are calculating this by subtracting starttime from endtime
            ' and dividing by 1000 (since the tickcount is in miliseconds.. 1000 ms = 1 sec)
            Dim secs As Double = Math.Floor(endtime - starttime) / 1000

            ' round the number of secs and remove the decimal point
            Dim secs2 As Double = Math.Round(secs, 0)

            ' calculate download rate in kb per sec.
            ' this is done by dividing 1024 by the number of seconds it
            ' took to download the file (1024 bytes = 1 kilobyte)
            Dim kbsec As Double = Math.Round(1024 / secs)

            NewText = ("2MB download: " & Chr(9) & secs2 & " secs (" & secs & "secs)" & vbNewLine & "Download rate: " & Chr(9) & kbsec & " KB/sec")

        Catch WebException As Exception
            Dim vd As New VDialog()
            With vd
                .WindowTitle = "Unable to resolve server"
                .MainIcon = VDialogIcon.Information
                .Buttons = New VDialogButton() {New VDialogButton(VDialogResult.OK)}
                .DefaultButton = VDialogDefaultButton.Button1
                .MainInstruction = "Unable to resolve server"
                .Content = "Sorry we were unable to locate the server" & vbNewLine & "this may because the server is temporerley offline or " + vbNewLine + "you do not have a connection to the internet"
                .Sound = VDialogSound.Error
                .Show()
            End With
            NewText = ("Error")
        End Try
        ' Check if this method is running on a different thread
        ' than the thread that created the control.
        If Me.rtbResults.InvokeRequired Then
            ' It's on a different thread, so use Invoke.
            Dim d As New SetTextCallback(AddressOf SetText)
            Me.Invoke(d, New Object() {[NewText]})
        Else
            ' It's on the same thread, no need for Invoke.
            Me.rtbResults.Text = [NewText]
        End If
        ExceptionHandling.UnhandledExceptionManager.RemoveHandler()
    End Sub

    Private Sub SetText(ByVal [text] As String)
        Me.rtbResults.Text = [text]
    End Sub

    Private Sub frmInternetSpeed_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

